package org.luckystrophe.snowtoms.customview;

import org.luckystrophe.snowtoms.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EView;

@EView
public class MovingImageView extends View {

	private AnimateDrawable mDrawable;

	public MovingImageView(Context context) {
		super(context);
		initView(context);
	}

	public MovingImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public MovingImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	private void initView(Context context) {
		setFocusable(true);
		setFocusableInTouchMode(true);

		int width = getContext().getResources().getDisplayMetrics().widthPixels;
		final int height = getContext().getResources().getDisplayMetrics().heightPixels;

		Drawable dr = context.getResources().getDrawable(R.drawable.writing1);
		dr.setBounds(0, 0, dr.getIntrinsicWidth(), height);

		Animation translateAni = new TranslateAnimation(0, -(dr.getIntrinsicWidth() - width), 0, 0);
		translateAni.setDuration(50000);
		translateAni.initialize(10, 10, 10, 10);
		mDrawable = new AnimateDrawable(dr, translateAni);
	}

	public void stopAnimation() {
		mDrawable.getAnimation().cancel();
	}

	@Override
	@Background
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		mDrawable.draw(canvas);
		invalidate();
	}

}
